<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>任务承接详情</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div th:replace="~{fragments/header :: header}"></div>
    
    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>任务承接详情</h2>
            <a th:href="@{/employer/task-applications}" class="btn btn-secondary">返回列表</a>
        </div>
        
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">承接信息</h5>
                        <table class="table table-borderless">
                            <tr>
                                <th>承接编号:</th>
                                <td th:text="${taskApplication.applicationId}">承接编号</td>
                            </tr>
                            <tr>
                                <th>任务编号:</th>
                                <td th:text="${taskApplication.taskId}">任务编号</td>
                            </tr>
                            <tr>
                                <th>威客编号:</th>
                                <td th:text="${taskApplication.freelancerId}">威客编号</td>
                            </tr>
                            <tr>
                                <th>承接时间:</th>
                                <td th:text="${#temporals.format(taskApplication.applyTime, 'yyyy-MM-dd HH:mm')}">承接时间</td>
                            </tr>
                            <tr>
                                <th>交付时间:</th>
                                <td th:text="${#temporals.format(taskApplication.deliveryTime, 'yyyy-MM-dd HH:mm')}">交付时间</td>
                            </tr>
                            <tr>
                                <th>交付状态:</th>
                                <td>
                                    <span th:if="${taskApplication.deliveryStatus == 'not_delivered'}" class="badge bg-primary">未交付</span>
                                    <span th:if="${taskApplication.deliveryStatus == 'delivered'}" class="badge bg-warning">已交付</span>
                                    <span th:if="${taskApplication.deliveryStatus == 'confirmed'}" class="badge bg-success">已确认</span>
                                    <span th:if="${taskApplication.deliveryStatus == 'completed'}" class="badge bg-secondary">已完成</span>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                
                <!-- 附件列表 -->
                <div class="card mt-4">
                    <div class="card-body">
                        <h5 class="card-title">交付附件</h5>
                        <div class="table-responsive">
                            <table class="table" th:if="${attachments != null and not #lists.isEmpty(attachments)}">
                                <thead>
                                    <tr>
                                        <th>文件名</th>
                                        <th>文件大小</th>
                                        <th>上传时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="attachment : ${attachments}" th:if="${attachment != null}">
                                        <td th:text="${attachment.fileName}">文件名</td>
                                        <td th:text="${#numbers.formatDecimal(attachment.fileSize / 1024.0, 1, 2) + ' KB'}">文件大小</td>
                                        <td th:text="${#temporals.format(attachment.uploadTime, 'yyyy-MM-dd HH:mm')}">上传时间</td>
                                        <td>
                                            <!-- 修改为超链接形式，可以直接查看文件 -->
                                            <a th:href="@{/uploads/} + ${#strings.arraySplit(attachment.filePath, '/')[#arrays.length(#strings.arraySplit(attachment.filePath, '/')) - 1]}" 
                                               th:text="${attachment.fileName}" 
                                               target="_blank" 
                                               class="me-2">文件名</a>
                                            <a th:href="@{/employer/task-applications/attachment/{id}(id=${attachment.attachmentId})}" 
                                               class="btn btn-sm btn-primary">下载</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <div class="alert alert-info" th:if="${attachments == null or #lists.isEmpty(attachments)}">
                                暂无附件
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 评价区域 -->
                <div class="card mt-4">
                    <div class="card-body">
                        <h5 class="card-title">任务评价</h5>
                        
                        <!-- 评价表单 -->
                        <form th:action="@{/task-reviews}" method="post" th:if="${taskApplication.deliveryStatus == 'confirmed'}">
                            <input type="hidden" name="taskId" th:value="${taskApplication.taskId}">
                            <input type="hidden" name="applicationId" th:value="${taskApplication.applicationId}">
                            
                            <div class="mb-3">
                                <label for="rating" class="form-label">评分</label>
                                <select class="form-control" id="rating" name="rating" required>
                                    <option value="">请选择评分</option>
                                    <option value="1">1星</option>
                                    <option value="2">2星</option>
                                    <option value="3">3星</option>
                                    <option value="4">4星</option>
                                    <option value="5">5星</option>
                                </select>
                            </div>
                            
                            <div class="mb-3">
                                <label for="content" class="form-label">评价内容</label>
                                <textarea class="form-control" id="content" name="content" rows="4" placeholder="请输入您的评价内容" required></textarea>
                            </div>
                            
                            <button type="submit" class="btn btn-primary">提交评价</button>
                        </form>
                        
                        <div th:if="${taskApplication.deliveryStatus != 'confirmed'}" class="alert alert-info">
                            需要雇主确认交付后才能进行评价
                        </div>
                        
                        <!-- 已有评价展示 -->
                        <div class="mt-4" th:if="${reviews != null and !#lists.isEmpty(reviews)}">
                            <h6>已有评价:</h6>
                            <div class="border rounded p-3 mb-3" th:each="review : ${reviews}">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <span th:each="i : ${#numbers.sequence(1, 5)}">
                                            <span th:if="${i <= review.rating}" class="text-warning">★</span>
                                            <span th:unless="${i <= review.rating}" class="text-muted">☆</span>
                                        </span>
                                    </div>
                                    <small class="text-muted" th:text="${#temporals.format(review.reviewTime, 'yyyy-MM-dd HH:mm')}">评价时间</small>
                                </div>
                                <p class="mt-2 mb-0" th:text="${review.content}">评价内容</p>
                                <div class="mt-2 text-end">
                                    <form th:action="@{/task-reviews/delete/{reviewId}(reviewId=${review.reviewId})}" method="post" style="display: inline;">
                                        <input type="hidden" name="applicationId" th:value="${taskApplication.applicationId}" />
                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除这条评价吗？')">删除评价</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 评价按钮 -->
                        <div class="mt-4 text-end" th:if="${taskApplication.deliveryStatus == 'confirmed' and (reviews == null or #lists.isEmpty(reviews))}">
                            <a th:href="@{/task-reviews/create(taskId=${taskApplication.taskId}, applicationId=${taskApplication.applicationId})}" 
                               class="btn btn-primary">
                                评价
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div th:replace="~{fragments/footer :: footer}"></div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>